package fr.xgouchet.wedding.client.view;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

import fr.xgouchet.wedding.client.i18n.Constants;
import fr.xgouchet.wedding.client.presenter.AgendaPresenter.FormDisplay;
import fr.xgouchet.wedding.client.service.TasksService;
import fr.xgouchet.wedding.shared.model.Task;

/**
 * UI form to edit a task
 * 
 * @author x.gouchet
 * 
 */
public class TaskFormView extends DialogBox implements FormDisplay {

	/**
	 * Default Constructor
	 */
	public TaskFormView() {
		// Set the dialog box's caption.
		setText(Constants.I18N.taskFormCaption());
		setModal(true);

		// create buttons
		mSaveButton = new Button(Constants.I18N.buttonSave());
		mCancelButton = new Button(Constants.I18N.buttonCancel());

		// create panels
		VerticalPanel main = new VerticalPanel();
		HorizontalPanel buttons = new HorizontalPanel();
		FlexTable form = new FlexTable();

		// Create input widgets
		mID = "";
		mTitle = new TextBox();
		mCategory = new SuggestBox(TasksService.getTasksCategories());
		mDescription = new TextArea();
		mDeadline = new DateBox();
		mDeadline.setFormat(new DateBox.DefaultFormat(DateTimeFormat
				.getFormat(Constants.I18N.dateFormat())));
		mStatus = new CheckBox();

		// create form
		form.setText(0, 0, Constants.I18N.labelTitle());
		form.setWidget(0, 1, mTitle);
		form.setText(1, 0, Constants.I18N.labelCategory());
		form.setWidget(1, 1, mCategory);
		form.setText(2, 0, Constants.I18N.labelDescription());
		form.setWidget(2, 1, mDescription);
		form.setText(3, 0, Constants.I18N.labelDeadline());
		form.setWidget(3, 1, mDeadline);
		form.setText(4, 0, Constants.I18N.labelIsTaskComplete());
		form.setWidget(4, 1, mStatus);

		// assemble panels
		buttons.add(mSaveButton);
		buttons.add(mCancelButton);

		// assemble main panel
		main.add(form);
		main.add(buttons);

		// add panel to dialog box
		add(main);
	}

	/**
	 * @see FormDisplay#getSaveButton()
	 */
	public HasClickHandlers getSaveButton() {
		return mSaveButton;
	}

	/**
	 * @see FormDisplay#getCancelButton()
	 */
	public HasClickHandlers getCancelButton() {
		return mCancelButton;
	}

	/**
	 * @see FormDisplay#displayTask(Task)
	 */
	public void displayTask(Task t) {
		mID = t.getID();
		mTitle.setText(t.getTitle());
		mDescription.setText(t.getDescription());
		mDeadline.setValue(t.getDeadline());
		mCategory.setValue(t.getCategory());
		mStatus.setValue(t.isComplete());
	}

	/**
	 * @see FormDisplay#getTask()
	 */
	public Task getTask() {
		Task task = new Task();
		task.setID(mID);
		task.setTitle(mTitle.getValue());
		task.setDescription(mDescription.getValue().replaceAll("\n", ""));
		task.setDeadline(mDeadline.getValue());
		task.setCategory(mCategory.getValue());
		task.setStatus(mStatus.getValue());
		return task;
	}

	/**
	 * @see FormDisplay#startForm()
	 */
	public void startForm() {
		center();
		mTitle.setFocus(true);
	}

	/**
	 * @see FormDisplay#stopForm()
	 */
	public void stopForm() {
		hide();
	}

	/**
	 * @see FormDisplay#asWidget()
	 */
	public Widget asWidget() {
		// TODO Auto-generated method stub
		return this;
	}

	/** the Save button */
	private Button mSaveButton;

	/** the Cancel button */
	private Button mCancelButton;

	/** The id of the guest */
	private String mID;

	/** The deadline */
	private DateBox mDeadline;

	/** The title of the Task */
	private TextBox mTitle;

	/** The description of the Task */
	private TextArea mDescription;

	/** The category of the task */
	private SuggestBox mCategory;

	/** the status (complete) of the task */
	private CheckBox mStatus;

}
